package com.androidhive.loginandregister;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends FragmentActivity {

	Button btnSignIn, btnSignUp;
	LoginDataBaseAdapter loginDataBaseAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// Get The Reference Of Buttons 
		btnSignIn = (Button) findViewById(R.id.buttonSignIn);
		btnSignUp = (Button) findViewById(R.id.buttonCreateAccount);

		// create a instance of SQLite Database
		loginDataBaseAdapter = new LoginDataBaseAdapter(this);
		loginDataBaseAdapter = loginDataBaseAdapter.open();

		// Set OnClick Listener on SignUp button

		TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

		// Listening to register new account link
		registerScreen.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Switching to Register screen
				Intent i = new Intent(getApplicationContext(),
						RegisterActivity.class);
				startActivity(i);
			}
		});

		// get the References of views
		final  EditText editTextUserName=(EditText) findViewById(R.id.editTextUserNameToLogin);
		final  EditText editTextPassword=(EditText) findViewById(R.id.editTextPasswordToLogin);
		Button btnSignIn=(Button) findViewById(R.id.buttonSignIn);
		btnSignIn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// get The User name and Password
				String userName = editTextUserName.getText().toString();
				String password = editTextPassword.getText().toString();

				// fetch the Password form database for respective user name
				String storedPassword = loginDataBaseAdapter
						.getSinlgeEntry(userName);

				// check if the Stored password matches with Password entered by
				// user
				if (password.equals(storedPassword)) {
					//Toast.makeText(LoginActivity.this,
					//		"Congrats: Login Successfull", Toast.LENGTH_LONG)
					//		.show();

					Intent i = new Intent(getApplicationContext(), MainActivity.class);
					i.putExtra("new_variable_name",userName);
					startActivity(i);
					


				} else {
					Toast.makeText(LoginActivity.this,
							"User Name or Password does not match",
							Toast.LENGTH_LONG).show();
				}
			}
		});

	}

	// Methods to handleClick Event of Sign In Button
	public void signIn(View V) {
		final Dialog dialog = new Dialog(LoginActivity.this);
		dialog.setContentView(R.layout.login);
		dialog.setTitle("Login");

		// get the References of views
		final EditText editTextUserName = (EditText) dialog
				.findViewById(R.id.editTextUserNameToLogin);
		final EditText editTextPassword = (EditText) dialog
				.findViewById(R.id.editTextPasswordToLogin);
		Button btnSignIn = (Button) dialog.findViewById(R.id.buttonSignIn);

		// Set On ClickListener
		btnSignIn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// get The User name and Password
				String userName = editTextUserName.getText().toString();
				String password = editTextPassword.getText().toString();

				// fetch the Password form database for respective user name
				String storedPassword = loginDataBaseAdapter
						.getSinlgeEntry(userName);

				// check if the Stored password matches with Password entered by
				// user
				if (password.equals(storedPassword)) {
					Toast.makeText(LoginActivity.this,
							"Congrats: Login Successfull", Toast.LENGTH_LONG)
							.show();

				} else {
					Toast.makeText(LoginActivity.this,
							"User Name or Password does not match",
							Toast.LENGTH_LONG).show();
				}
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Close The Database
		loginDataBaseAdapter.close();
	}

}